# -*- coding: utf-8 -*-
from datetime import timedelta
from time import time
from utils.operators.rich_sql_sensor import RichSqlSensor
from jms.dm.dm_whole_decide_duty_dt import jms_dm__dm_whole_decide_duty_dt

label = f"dm_whole_decide_duty_dt_{int(time())}"
timeout = timedelta(minutes=30).seconds
doris_jms_dm__dm_whole_decide_duty_dt = RichSqlSensor(
    task_id='doris_jms_dm__dm_whole_decide_duty_dt',
    pool='broker_load_pool',
    email=['suning@jtexpress.com','yl_bigdata@yl-scm.com'],
    conn_id='doris',
    pre_sql=f"""
    TRUNCATE TABLE jms_dm.dm_whole_decide_duty_dt PARTITION (
         p{{{{ execution_date | date_add(-10) | cst_ds_nodash }}}} 
        ,p{{{{ execution_date | date_add(-9) | cst_ds_nodash }}}}
        ,p{{{{ execution_date | date_add(-8) | cst_ds_nodash }}}}
        ,p{{{{ execution_date | date_add(-7) | cst_ds_nodash }}}}
        ,p{{{{ execution_date | date_add(-6) | cst_ds_nodash }}}}
        ,p{{{{ execution_date | date_add(-5) | cst_ds_nodash }}}} 
        ,p{{{{ execution_date | date_add(-4) | cst_ds_nodash }}}}
        ,p{{{{ execution_date | date_add(-3) | cst_ds_nodash }}}}
        ,p{{{{ execution_date | date_add(-2) | cst_ds_nodash }}}}
        ,p{{{{ execution_date | date_add(-1) | cst_ds_nodash }}}}
        ,p{{{{ execution_date | date_add(+0) | cst_ds_nodash }}}}
        ,p{{{{ execution_date | date_add(+1) | cst_ds_nodash }}}}
        ,p{{{{ execution_date | date_add(+2) | cst_ds_nodash }}}}
        ,p{{{{ execution_date | date_add(+3) | cst_ds_nodash }}}}
        ,p{{{{ execution_date | date_add(+4) | cst_ds_nodash }}}}
        ,p{{{{ execution_date | date_add(+5) | cst_ds_nodash }}}}
    );
    LOAD LABEL jms_dm.{label} (
        DATA INFILE(
            "hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_whole_decide_duty_dt/dt={{{{ execution_date | date_add(-10) | cst_ds }}}}/*",
            "hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_whole_decide_duty_dt/dt={{{{ execution_date | date_add(-9) | cst_ds }}}}/*",
            "hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_whole_decide_duty_dt/dt={{{{ execution_date | date_add(-8) | cst_ds }}}}/*",
            "hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_whole_decide_duty_dt/dt={{{{ execution_date | date_add(-7) | cst_ds }}}}/*",
            "hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_whole_decide_duty_dt/dt={{{{ execution_date | date_add(-6) | cst_ds }}}}/*",
            "hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_whole_decide_duty_dt/dt={{{{ execution_date | date_add(-5) | cst_ds }}}}/*",
            "hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_whole_decide_duty_dt/dt={{{{ execution_date | date_add(-4) | cst_ds }}}}/*",
            "hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_whole_decide_duty_dt/dt={{{{ execution_date | date_add(-3) | cst_ds }}}}/*",
            "hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_whole_decide_duty_dt/dt={{{{ execution_date | date_add(-2) | cst_ds }}}}/*",
            "hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_whole_decide_duty_dt/dt={{{{ execution_date | date_add(-1) | cst_ds }}}}/*",
            "hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_whole_decide_duty_dt/dt={{{{ execution_date | date_add(+0) | cst_ds }}}}/*",
            "hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_whole_decide_duty_dt/dt={{{{ execution_date | date_add(+1) | cst_ds }}}}/*",
            "hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_whole_decide_duty_dt/dt={{{{ execution_date | date_add(+2) | cst_ds }}}}/*",
            "hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_whole_decide_duty_dt/dt={{{{ execution_date | date_add(+3) | cst_ds }}}}/*",
            "hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_whole_decide_duty_dt/dt={{{{ execution_date | date_add(+4) | cst_ds }}}}/*",
            "hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_whole_decide_duty_dt/dt={{{{ execution_date | date_add(+5) | cst_ds }}}}/*"
        )
        INTO TABLE dm_whole_decide_duty_dt
        FORMAT AS 'PARQUET'(       
             waybill_no
            ,platform_effect
            ,ziyou_platform_effect
            ,if_platform_standard
            ,if_doudi_platform_standard
            ,cainiao_platform_name
            ,cainiao_platform_effect
            ,if_cainiao_platform_pres_standard
            ,first_duty_type
            ,first_ziyou_duty_type
            ,cainiao_plan_sign_time
            ,ziyou_plan_sign_time
            ,order_source_code
            ,order_source_name
            ,if_intercept
            ,if_reback
            ,if_transfer
            ,if_taking_off
            ,if_taitc
            ,if_reback_reach
            ,start_city_code
            ,start_city_name
            ,sign_city_code
            ,sign_city_name
            ,taking_time
            ,deliver_time
            ,aging_sign_time
            ,actual_sign_time
            ,reback_apply_time
            ,intercept_registration_time
            ,economic_circle_name
            ,scan_site_no
            ,scan_site_code
            ,scan_site_name
            ,scan_site_type
            ,scan_site_arrival_time
            ,scan_site_send_time
            ,scan_site_actual_arrival_time
            ,scan_site_actual_send_time
            ,scan_site_seal_time
            ,scan_site_planned_departure_time
            ,scan_site_planned_arrival_time
            ,scan_site_actual_shipment_no
            ,scan_site_actual_shipment_name
            ,scan_site_arrival_bagg_code
            ,scan_site_arrival_bagg_name
            ,scan_site_send_bagg_code
            ,scan_site_send_bagg_name
            ,scan_site_send_bagg_addr_code
            ,scan_site_send_bagg_addr_name
            ,scan_site_arrival_bagg_addr_code
            ,scan_site_arrival_bagg_addr_name
            ,scan_site_if_stop
            ,next_site_code
            ,next_site_name
            ,next_site_type
            ,actual_diff_time
            ,actual_node
            ,actual_effect
            ,first_network_code
            ,first_network_name
            ,sign_network_code
            ,sign_network_name
            ,last_network_code
            ,last_network_name
            ,start_center_code
            ,start_center_name
            ,end_center_code
            ,end_center_name
            ,plan_last_taking_time
            ,plan_sign_time
            ,scan_site_plan_send_time
            ,scan_site_plan_arrival_time
            ,scan_site_if_static
            ,next_site_if_static
            ,total_days
            ,route_id
            ,duty_type
            ,ziyou_duty_type
        )
        columns from path as (dt)
        set(
             waybill_no                             = waybill_no
            ,platform_effect                        = platform_effect
            ,ziyou_platform_effect                  = ziyou_platform_effect
            ,if_platform_standard                   = if_platform_standard
            ,if_doudi_platform_standard             = if_doudi_platform_standard
            ,cainiao_platform_name                  = cainiao_platform_name
            ,cainiao_platform_effect                = cainiao_platform_effect
            ,if_cainiao_platform_pres_standard      = if_cainiao_platform_pres_standard
            ,first_duty_type                        = first_duty_type
            ,first_ziyou_duty_type                  = first_ziyou_duty_type
            ,cainiao_plan_sign_time                 = cainiao_plan_sign_time
            ,ziyou_plan_sign_time                   = ziyou_plan_sign_time
            ,order_source_code                      = order_source_code
            ,order_source_name                      = order_source_name
            ,if_intercept                           = if_intercept
            ,if_reback                              = if_reback
            ,if_transfer                            = if_transfer
            ,if_taking_off                          = if_taking_off
            ,if_taitc                               = if_taitc
            ,if_reback_reach                        = if_reback_reach
            ,start_city_code                        = start_city_code
            ,start_city_name                        = start_city_name
            ,sign_city_code                         = sign_city_code
            ,sign_city_name                         = sign_city_name
            ,taking_time                            = taking_time
            ,deliver_time                           = deliver_time
            ,aging_sign_time                        = aging_sign_time
            ,actual_sign_time                       = actual_sign_time
            ,reback_apply_time                      = reback_apply_time
            ,intercept_registration_time            = intercept_registration_time
            ,economic_circle_name                   = economic_circle_name
            ,scan_site_no                           = scan_site_no
            ,scan_site_code                         = scan_site_code
            ,scan_site_name                         = scan_site_name
            ,scan_site_type                         = scan_site_type
            ,scan_site_arrival_time                 = scan_site_arrival_time
            ,scan_site_send_time                    = scan_site_send_time
            ,scan_site_actual_arrival_time          = scan_site_actual_arrival_time
            ,scan_site_actual_send_time             = scan_site_actual_send_time
            ,scan_site_seal_time                    = scan_site_seal_time
            ,scan_site_planned_departure_time       = scan_site_planned_departure_time
            ,scan_site_planned_arrival_time         = scan_site_planned_arrival_time
            ,scan_site_actual_shipment_no           = scan_site_actual_shipment_no
            ,scan_site_actual_shipment_name         = scan_site_actual_shipment_name
            ,scan_site_arrival_bagg_code            = scan_site_arrival_bagg_code
            ,scan_site_arrival_bagg_name            = scan_site_arrival_bagg_name
            ,scan_site_send_bagg_code               = scan_site_send_bagg_code
            ,scan_site_send_bagg_name               = scan_site_send_bagg_name
            ,scan_site_send_bagg_addr_code          = scan_site_send_bagg_addr_code
            ,scan_site_send_bagg_addr_name          = scan_site_send_bagg_addr_name
            ,scan_site_arrival_bagg_addr_code       = scan_site_arrival_bagg_addr_code
            ,scan_site_arrival_bagg_addr_name       = scan_site_arrival_bagg_addr_name
            ,scan_site_if_stop                      = scan_site_if_stop
            ,next_site_code                         = next_site_code
            ,next_site_name                         = next_site_name
            ,next_site_type                         = next_site_type
            ,actual_diff_time                       = actual_diff_time
            ,actual_node                            = actual_node
            ,actual_effect                          = actual_effect
            ,first_network_code                     = first_network_code
            ,first_network_name                     = first_network_name
            ,sign_network_code                      = sign_network_code
            ,sign_network_name                      = sign_network_name
            ,last_network_code                      = last_network_code
            ,last_network_name                      = last_network_name
            ,start_center_code                      = start_center_code
            ,start_center_name                      = start_center_name
            ,end_center_code                        = end_center_code
            ,end_center_name                        = end_center_name
            ,plan_last_taking_time                  = plan_last_taking_time
            ,plan_sign_time                         = plan_sign_time
            ,scan_site_plan_send_time               = scan_site_plan_send_time
            ,scan_site_plan_arrival_time            = scan_site_plan_arrival_time
            ,scan_site_if_static                    = scan_site_if_static
            ,next_site_if_static                    = next_site_if_static
            ,total_days                             = total_days
            ,route_id                               = route_id
            ,duty_type                              = duty_type
            ,ziyou_duty_type                        = ziyou_duty_type
            ,dt                                     = dt
        )
    )
    WITH BROKER '{{{{ var.json.doris_brokers | random_choice }}}}'
    PROPERTIES ('timeout'='{timeout}', 'max_filter_ratio'='0.0')""",
    poke_sql=f"SHOW LOAD FROM jms_dm WHERE label = '{label}' ORDER BY CreateTime DESC LIMIT 1",
    sql_on_kill=f"CANCEL LOAD FROM jms_dm WHERE LABEL = '{label}'",
    success=lambda r: r[2] == 'FINISHED',
    failure=lambda r: (r[2] is not None and r[2] == 'CANCELLED', str(r[7])),
    poke_interval=60,
    execution_timeout=timedelta(seconds=timeout + 120), )

doris_jms_dm__dm_whole_decide_duty_dt << jms_dm__dm_whole_decide_duty_dt
